tongfusoft.dictionary = function() {
}
tongfusoft.dictionary.prototype = {

	dictClassTable : function() {

		var table = new tongfusoft.table();
		var columns = [
				{
					"data" : "dictclassId"
				},
				{
					"data" : "dictclassCode"
				},
				{
					"data" : "dictclassName"
				},
				{
					"data" : "lastUpdBy"
				},
				{
					"data" : "lastUpdDate"
				},
				{
					"data" : "dictclassId",
					"render" : function(data, type, full, meta) {
						var delbut = '<button dictClassId="'
								+ data
								+ '" onclick="new tongfusoft.dictionary().dictClassDel(this)" type="button" data-toggle="modal" data-target="#dellass" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
						var editbut = '<button dictClassId="'
								+ data
								+ '" onclick="new tongfusoft.dictionary().dictClassEdit(this)" type="button" data-toggle="modal" data-target="#delDictClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

						return editbut + delbut;
					}

				} ]

		table.dataTable('dictClassTable', 'dictionaryClass/query', columns, null);

		$('#dictClassTable').DataTable().on(
				'select',
				function(e, dt, type, indexes) {
					$("#dclassId").val($('#dictClassTable').DataTable().rows(indexes).data()[0].dictclassId);
					$("#dictTableTitle").text(
							$('#dictClassTable').DataTable().rows(indexes).data()[0].dictclassName + ":字典表值");

					$('#dictTable').DataTable().clear();
					$('#dictTable').DataTable().ajax.url(
							common.getContextPath('dictionary/query?dictClassId=' + $("#dclassId").val())).load();
				});

	},

	dictTable : function() {

		var table = new tongfusoft.table();
		var columns = [
				{
					"data" : "dictId"
				},
				{
					"data" : "dictkeyName"
				},
				{
					"data" : "dictkeyValue"
				},
				{
					"data" : "showOrder"
				},
				{
					"data" : "lastUpdBy"
				},
				{
					"data" : "lastUpdDate"
				},
				{

					"data" : "dictId",
					"render" : function(data, type, full, meta) {
						var delbut = '<button dictId="'
								+ data
								+ '" onclick="new tongfusoft.dictionary().dictDel(this)" type="button" data-toggle="modal" data-target="#dellass" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
						var editbut = '<button dictId="'
								+ data
								+ '" onclick="new tongfusoft.dictionary().dictEdit(this)" type="button" data-toggle="modal" data-target="#delDictClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

						return editbut + delbut;
					}

				} ]

		table.dataTable('dictTable', 'dictionary/query?dictClassId=' + $("#dclassId").val(), columns, null);

	},
	addBtnDictClass : function() {
		$("#addBtnDictClass").click(function() {
			$("#dictClassModelTitle").text('新增');
			$('#dictclassCode').attr("disabled",false);
			common.clean("dictClassForm");
			$('#dictClassModel').modal('show');
			$("#oper").val("add");

		})

	},
	dictClasssSubmit : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("dictClassForm");
				var url = 'dictionaryClass/add';

				if ($("#oper").val() == 'edit') {

					url = 'dictionaryClass/edit';
					initData['dictclassId'] = $("#dclassId").val();
				}
				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {

						$('#dictClassModel').modal('hide');
						$('#dictClassTable').DataTable().ajax.reload();

						$('#dictTable').DataTable().ajax.url(
								common.getContextPath('dictionary/query?dictClassId=' + $("#dclassId").val())).load();

					} else if (data.code == 'repeat_error') {

						toastr["error"]('编码不能重复!');
					}else{
						toastr["error"]('保存失败!');
					}

				}, $("#oper").val())
			}
		});
		$("#dictClassForm").validate();

		$("#submitBtn").click(function() {

			$("#dictClassForm").submit();
		})
	},
	// 删除字典分类
	dictClassDel : function(obj) {

		swal({
			title : "是否确认?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#DD6B55",
			confirmButtonText : "删除",
			cancelButtonText : "取消",
			closeOnConfirm : true
		}, function() {
			common.ajaxSubmitOnSuccess({
				"dictclassId" : $(obj).attr("dictClassId")
			}, 'dictionaryClass/del', function(data) {

				if (data.state == 'success') {

					$('#dictClassTable').DataTable().ajax.reload();
					$('#dictTable').DataTable().ajax.reload();
				}else{
					toastr["error"]('删除失败!');
				}

			}, "del")
		});

	},
	// 打开字典分类修改页
	dictClassEdit : function(obj) {
		$('#dictclassCode').attr("disabled",true);
		common.post('dictionaryClass/detail', {
			"dictclassId" : $(obj).attr("dictClassId")
		}, function(data) {
			$("#dictClassModelTitle").text('修改')
			$('#dictClassModel').modal('show');
			$("#oper").val("edit");
			if (data.state == 'success') {

				$('#dictclassCode').val(data.data.dictclassCode);
				$('#dictclassName').val(data.data.dictclassName);
				$('#dclassId').val(data.data.dictclassId);
			}

		});

	},
	addBtnDict : function() {
		$("#addBtnDict").click(function() {
			if ($('#dclassId').val() == '') {
				toastr["error"]('请先选着');
				return false;
			}
			$("#dictModelTitle").text('新增');
			common.clean("dictForm");
			$('#dictModel').modal('show');
			$("#oper").val("add");

		})

	},
	dictSubmit : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("dictForm");
				var url = 'dictionary/add';
				initData['dictclassId'] = $("#dclassId").val();

				if ($("#oper").val() == 'edit') {

					url = 'dictionary/edit';
					initData['dictcId'] = $("#dictId").val();
				}
				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {

						$('#dictModel').modal('hide');
						$('#dictTable').DataTable().ajax.reload();
					} else if (data.code == 'repeat_error') {

						toastr["error"]('编码不能重复!');
					}else{
						toastr["error"]('保存失败!');
					}
				}, $("#oper").val())
			}
		});
		$("#dictForm").validate();

		$("#submitDictBtn").click(function() {

			$("#dictForm").submit();
		})
	},

	// 删除字典值
	dictDel : function(obj) {

		swal({
			title : "是否确认?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#DD6B55",
			confirmButtonText : "删除",
			cancelButtonText : "取消",
			closeOnConfirm : true
		}, function() {
			common.ajaxSubmitOnSuccess({
				"dictId" : $(obj).attr("dictId")
			}, 'dictionary/del', function(data) {

				if (data.state == 'success') {

					$('#dictTable').DataTable().ajax.reload();
				}else{
					toastr["error"]('删除失败!');
				}

			}, "del")
		});

	},
	// 打开字典值修改页
	dictEdit : function(obj) {

		common.post('dictionary/detail', {
			"dictId" : $(obj).attr("dictId"),
			"dictclassId" : $("#dclassId").val()
		}, function(data) {
			$("#dictModelTitle").text('修改')
			$('#dictModel').modal('show');
			$("#oper").val("edit");
			if (data.state == 'success') {

				$('#dictkeyName').val(data.data.dictkeyName);
				$('#dictkeyValue').val(data.data.dictkeyValue);
				$('#showOrder').val(data.data.showOrder);

				$('#dictId').val(data.data.dictId);
			}

		});

	},
	search : function() {

		$("#searchBtn").click(function() {

			$('#dictClassTable').DataTable().ajax.reload();

		})

	}
}

$(function() {

	var dict = new tongfusoft.dictionary();
	// 字典表主表表格
	dict.dictClassTable();

	// 字典表从表表格
	dict.dictTable();

	// /提交字典分类数据 用于修改和新增提交数据
	dict.dictClasssSubmit();

	// 字典分类新增页
	dict.addBtnDictClass();

	// 字典值新增页
	dict.addBtnDict();
	// 提交字典值数据 用于修改和新增提交数据
	dict.dictSubmit();
	
	dict.search();
})